
Cesium.Ion.defaultAccessToken =
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxZDljNTllZi1lZjk3LTQ0Y2MtYWQxOC0yMTBjZWRiOWY0ODQiLCJpZCI6NDMxMzEsImlhdCI6MTY0MjQwMTQxNn0.9e2RbcXo2PXxegVNuN_1KZEvCl1-RBJXIPddOSs3F3w";

var viewer = new Cesium.Viewer("cesiumContainer", {
    contextOptions: {
        requestWebgl2: true,
    },
});
window.viewer = viewer
var scene = viewer.scene;

viewer.scene.globe.depthTestAgainstTerrain = true; // 地形深度与普通的图形深度进行测试

scene.screenSpaceCameraController.enableCollisionDetection = false; // 穿越地形

// viewer.scene.globe.show = false;
// viewer.scene.globe.enableLighting = true;
//
viewer.scene.sun.show = false;

viewer.scene.skyAtmosphere.show = false;

// 配置TOCC 项目服务器地址
let options = {
    scene: scene,
    viewer: viewer,
    enableOcclusion: false,
    quadtreeUrl: "http://183.248.209.32:12000/bim/webgl/projectsInTiles",
    tilesetUrl: "http://183.248.209.32:12000/bim/webgl/tilesetCache",
    manifestContentUrl: "http://183.248.209.32:12000/bim/rest/webgl/manifestWithBatchTable",
    projectIds: [1], // 松江 addr,
    oglServerUrl: "http://183.248.209.32:12000/bim/resource",
};

let quadtreeManager = new Cesium.BimrunQuadtreeManager(options);

viewer.scene.primitives.add(quadtreeManager);


// tocc项目视角
viewer.camera.flyTo({
    destination: new Cesium.Cartesian3(
        -2799202.799745468,
        4760001.9869112205,
        3181965.0711831623
    ),
    orientation: {
        direction: new Cesium.Cartesian3(
            -0.5992039315748787,
            -0.789807426855059,
            0.13099189619895946
        ),
        up: new Cesium.Cartesian3(
            -0.6172354740014094,
            0.5599460604833154,
            0.5527031562995375
        ),
    },
});

// test for flyController
// var flyControls = new Cesium.FlyControlHandler(viewer.scene);
//
// fetch('../CustomData/fly.json').then(response => response.json()).then(data => {
//
//     flyControls.cameraKeys = data;
//     flyControls.play();
// });
// madd end



// // test for style
var colorStyle = new Cesium.Cesium3DTileStyle({
    color: {
        conditions: [
            ["${id} > 100", "color('red')"],
            ["${id} > 50", "color('blue')"],
            ["${id} > 10", "color('yellow')"],
            [true, "color('cyan')"],
        ],
    },
});


const stylingOptions = [
    {
        text: "style off",
        onselect: function () {
            quadtreeManager.style = undefined;
        },
    },
    {
        text: "style on",
        onselect: function () {
            quadtreeManager.style = colorStyle;
        },
    },
];
Sandcastle.addToolbarMenu(stylingOptions, "toolbar");

// // madd end

// madd: test for picked feature
var oldPicked = undefined;
var oldColor = undefined;

var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
handler.setInputAction(function (e) {
    var picked = viewer.scene.pick(e.position);

    viewer.scene.requestRender();

    if (picked instanceof Cesium.Cesium3DTileFeature) {
        if (oldPicked !== undefined) {
            oldPicked.color = oldColor;
            // oldPicked.show = true; // 显示该模型
            if (oldPicked === picked) {
                oldPicked = undefined;
                return;
            }
        }
        oldPicked = picked;

        oldColor = new Cesium.Color.clone(picked.color); //(currentColor.r, currentColor.g, currentColor.b, currentColor.a);

        picked.color = new Cesium.Color(0, 0, 1, 0.5); // 透明测试
    }
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

